<template>
 <el-card>
     <!-- 搜索 -->
     <Search :model="searchForm" @search="getData" @reset="resetSearchForm">
        <SearchItem label="關鍵詞">
          <el-input v-model="searchForm.keyword" placeholder="管理員名稱" clearable></el-input>
        </SearchItem>
    </Search>

    <!-- 新增|刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"/>

    <el-table :data="tableData"  v-loading="loading">
      <el-table-column prop="id" label="序號" />
      <el-table-column prop="username" label="管理员名稱" />
     
      <el-table-column prop="username" label="所屬角色" >
        
        <template #default="{ row }">
          {{ row.role?row.role.role_name:'' }}
       
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="創建時間"  />
 
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <small v-if="scope.row.id == 1" class="text-sm text-gray-500">暫無操作</small>
          <div v-else>
            <el-button type="primary" size="small"  @click="handleEdit(scope.row)">修改</el-button>

          <el-popconfirm title="是否要删除该管理员？" confirmButtonText="確認" cancelButtonText="取消" @confirm="handleDelete(scope.row.id)">
                <template #reference>
                    <el-button type="danger" size="small">删除</el-button>
                </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
      
    </el-table>

    <div class="flex items-center justify-center mt-5">
        <el-pagination background layout="prev, pager ,next" :total="total" :current-page="currentPage" :page-size="limit" @current-change="getData"/>
    </div>

    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
        <el-form-item label="用戶名" prop="username">
          <el-input v-model="form.username" placeholder="用戶名"></el-input>
        </el-form-item>
        <el-form-item label="密碼" prop="password">
          <el-input v-model="form.password" type="password" placeholder="密碼"></el-input>
        </el-form-item>
      
        <el-form-item label="所屬角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="選擇所屬角色">
            <el-option v-for="item in roles"
              :key="item.id"
              :label="item.role_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      
      </el-form>
    </FormDrawer>
 </el-card>
</template>

 <script setup>
import { ref } from "vue"
import ListHeader from "@/components/ListHeader.vue";
import FormDrawer from "@/components/FormDrawer.vue";
import Search from "@/components/Search.vue";
import SearchItem from "@/components/SearchItem.vue";
import { useInitTable,useInitForm } from '@/composables/useCommon.js'


import {
  getadmin,
  del,
  update,
  create
} from "@/api/manager.js"
const roles = ref([])
const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  currentPage,
  total,
  limit,
  getData,
  handleDelete
} = useInitTable({
  searchForm:{
    keyword:""
  },
  getList:getadmin,
  onGetListSuccess:(res)=>{
    tableData.value = res.data.data
    total.value = res.data.total
    roles.value = res.roles
  },
  delete:del,
 
})

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit
} = useInitForm({
  form:{
      username: "",
      password: "",
      role_id: null,
      avatar: ""
  },
  getData,
  update:update,
  create:create
})



</script>

